package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.openapi2204.common.constants.RedisConstants;
import com.qf.openapi2204.common.mqbean.RoutingInfoMqBean;
import com.qf.openapi2204.common.utils.ObjectToHashMapConverter;
import com.qianfeng.openapi.web.master.feignclient.CacheClient;
import com.qianfeng.openapi.web.master.mapper.ApiMappingMapper;
import com.qianfeng.openapi.web.master.pojo.ApiMapping;
import com.qianfeng.openapi.web.master.service.ApiMappingService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ApiMappingServiceImpl implements ApiMappingService {
    @Autowired
    private ApiMappingMapper apiMappingMapper;

    @Autowired
    CacheClient cacheClient;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public void addApiMapping(ApiMapping mapping) {
        apiMappingMapper.addApiMapping(mapping);
        //添加到缓存
        try {
            //判断当前添加的数据是不是有效的数据
            if (mapping.getState() == 1) {
                //发送mq到网关
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateApiMapping(ApiMapping mapping) {
        apiMappingMapper.updateApiMapping(mapping);
        //更新分为以下情况,如果是原先是有效的数据,现在更新为无效的数据,则是从缓存中删除数据
        try {
            if (mapping.getState() == 1) {
                //如果更新的是其他内容(有效的) 则更新缓存中的数据
                String prefix = RedisConstants.APINAME_PREFIX+":";
                String id = mapping.getGatewayApiName();
                String key = prefix+id;
                cacheClient.hmset(key,mapping);

                // 写mq
                // 需要告诉网关哪些信息呢？
                // 1. 运营管理平台做的变动的类型：更改、新增、删除
                // 2. 路由信息
                RoutingInfoMqBean routingInfoMqBean = new RoutingInfoMqBean();

                routingInfoMqBean.setType("update");
                routingInfoMqBean.setKey(key);

                // java对象 --->  hashmap
                HashMap<String, Object> map = ObjectToHashMapConverter.convert(mapping);
                routingInfoMqBean.setData(map);

                // 使用默认交换机，给
                rabbitTemplate.convertAndSend("","openapi-routing-queue",routingInfoMqBean);

            }else{
                //从缓存中删除数据
            }
        } catch (Exception e) {

        }

    }

    @Override
    public PageInfo<ApiMapping> getMappingList(ApiMapping criteria, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return new PageInfo<>(apiMappingMapper.getMappingList(criteria));
    }

    @Override
    public ApiMapping getMappingById(int id) {
        return apiMappingMapper.getMappingById(id);
    }

    @Override
    public void deleteMapping(int[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (int id : ids) {
            ApiMapping mapping = apiMappingMapper.getMappingById(id);
            if (mapping != null) {
                mapping.setState(0);
                apiMappingMapper.updateApiMapping(mapping);
                try {
                    //从缓存中删除数据
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }


}
